package com.wdhcr.controller.admin;

import com.wdhcr.entity.Notice;
import com.wdhcr.enums.ResultCode;
import com.wdhcr.service.NoticeService;
import com.wdhcr.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

public class NoticeController {


    @Autowired
    private NoticeService noticeService;
    /*
     * @description: 保存公告的方法
     * @params: [link]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/23 23:39
     */
    @CrossOrigin
    @PostMapping("saveNotice")
    public ResponseResult saveNotice(@RequestBody Notice notice){
        Notice notice1 = noticeService.saveNotice(notice);
        if (notice1 != null){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"添加成功!",null);
        }
        return new ResponseResult(ResultCode.FAIL.getCode(),"添加失败!",null);
    }


    /*
     * @description: 根据id查询公告信息的接口
     * @params: [id]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/23 21:42
     */
    @CrossOrigin
    @GetMapping("getNoticeById/{id}")
    public ResponseResult getNoticeById(@PathVariable Long id){
        Notice notice = noticeService.getNotice(id);
        if (notice != null){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取成功！",notice);
        }
        return new ResponseResult(ResultCode.FAIL.getCode(),"获取失败！",null);
    }
    /*
     * @description:根据id删除对应公告
     * @params: [id]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/23 21:46
     */
    @CrossOrigin
    @GetMapping("deleteNoticeById/{id}")
    public ResponseResult deleteNoticeById(@PathVariable Long id) throws Exception {
        try {
            noticeService.deleteById(id);

            return new ResponseResult(ResultCode.SUCCESS.getCode(),"删除成功！",null);

        }catch (Exception e){
            throw  new Exception("删除失败!");
        }

    }


    /*
     * @description: 前端分页查询的条数的接口
     * @params: [page, size]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/21 21:14
     */
    @CrossOrigin
    @GetMapping("getNoticeList/{page}/{size}")
    public ResponseResult getNoticeList(@PathVariable("page") Integer page,@PathVariable("size") Integer size){

        Sort sort = Sort.by(Sort.Direction.DESC,"weight");
        PageRequest pageRequest = PageRequest.of(page, size,sort);
        Page<Notice> all = noticeService.listNotice(pageRequest);
        if (!all.isEmpty()){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取列表成功！",all);
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(),"暂无列表信息！",all);

    }

    @CrossOrigin
    @GetMapping("getNotice")
    public ResponseResult getNotice(){
        Sort sort = Sort.by(Sort.Direction.DESC,"weight");
        PageRequest pageRequest = PageRequest.of(0, 1,sort);
        List<Notice> notices = noticeService.listNotice(pageRequest).toList();
        if (!notices.isEmpty()){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取公告成功！",notices);
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(),"暂无公告信息！",notices);



    }


}
